package tcpserver;

import io.netty.channel.Channel;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

public class RFIDSessionManager {

    private final static ConcurrentHashMap<String, RFIDSession> store = new ConcurrentHashMap<>();

    public final static RFIDSessionManager instance = new RFIDSessionManager();

    private RFIDSessionManager() { }

    public void remove(Channel channel) {

        store.remove(keyOfChannel(channel));
    }

    public void computeIfAbsent(Channel channel, Function<? super String, ? extends RFIDSession> mappingFunction) {

        store.computeIfAbsent(keyOfChannel(channel), mappingFunction);
    }

    public RFIDSession get(Channel channel) {

        return store.get(keyOfChannel(channel));
    }

    private String keyOfChannel(Channel channel) {

        return channel.remoteAddress().toString() + ":" + channel.id().asLongText();
    }
}
